//
//  BuyDetailPopViewController.m
//  体育商城
//
//  Created by  on 2022/9/23.
//

#import "BuyDetailPopViewController.h"
#import "GoodsStyleCell.h"
#import "BuyDetailFooterView.h"

@interface BuyDetailPopViewController ()
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet UIView *middleView;
@property (strong, nonatomic) IBOutlet UIImageView *img;
@property (strong, nonatomic) IBOutlet UIButton *addressBtn;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *middleViewH;
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) IBOutlet UILabel *money;
@property (strong, nonatomic) NSURLSessionTask *task;

@end

@implementation BuyDetailPopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    if (self.task) {
        [self.task cancel];
    }
}
- (void)initUI{
    CGRect bounds = CGRectMake(0, 0, SCREEN_WIDTH, self.bgView.bounds.size.height);
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(20, 20)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.frame = bounds;
    layer.path = path.CGPath;
    self.bgView.layer.mask = layer;
    
    [self.collectionView registerNib:[GoodsStyleCell nib] forCellWithReuseIdentifier:[GoodsStyleCell ID]];
    [self.collectionView registerNib:[BuyDetailFooterView nib] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[BuyDetailFooterView ID]];
    
    self.middleView.hidden = self.model.orderStatus == GoodsOrderStastusJoinCart;
}
- (void)setModel:(Goods *)model{
    if (_model != model) {
        _model = model;
        self.img.image = nil;
        self.money.text = [NSString stringWithFormat:LS(@"¥:%@"),model.price];
        self.task = [UIImage imageWithURL:model.mainImage block:^(UIImage * _Nonnull image) {
            self.img.image = image;
        }];
        self.middleView.hidden = model.orderStatus == GoodsOrderStastusJoinCart;
        self.middleViewH.constant = self.middleView.hidden ? 0 : 45;
        if (self.middleView.hidden == NO) {
            NSString *address = self.model.address ? self.model.address.address : LS(@"去选择收获地址");
            [self.addressBtn setTitle:address forState:UIControlStateNormal];
        }
        [self.collectionView reloadData];
    }
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {///取消
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{///地址按钮
        AddressManagerViewController * vc = [AddressManagerViewController new];
        BaseNavigationController * navc = [[BaseNavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:navc animated:YES completion:nil];
    }
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return  1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsStyleCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:[GoodsStyleCell ID] forIndexPath:indexPath];
    cell.model = self.model;
    return  cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    BuyDetailFooterView * view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[BuyDetailFooterView ID] forIndexPath:indexPath];
    view.model = self.model;
    WS(self)
    view.block = ^{
        weakself.block();
    };
    return view;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(SCREEN_WIDTH - 20, 32);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return  UIEdgeInsetsMake(0, 10, 10, 20);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(SCREEN_WIDTH - 20, 270);
}


@end
